package com.example.yichuangyun.mvvm.ui.activity.order.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.StatusModel;
import com.example.yichuangyun.model.order.AttachmentModel;
import com.example.yichuangyun.model.order.FileTypeModel;
import com.example.yichuangyun.model.order.OrderAddFollowParamsModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.utils.ToastHelper;
import com.example.yichuangyun.utils.treelist.SetShareBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.MultipartBody;

public class OrderAddFollowViewModel extends BaseViewModel {

    public MutableLiveData<String> content = new MutableLiveData<>();
    public MutableLiveData<List<FileTypeModel>> fileTypeMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<AttachmentModel> attachmentModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> mAddSuccessData = new MutableLiveData<>();
    public final MutableLiveData<SetShareBean> treeData = new MutableLiveData<>();

    /**
     * 附件类型
     */
    public void getFileType() {
        addSubscribe(mRetrofitHelper.getFileType().subscribeWith(new ResourceSubscriber<List<FileTypeModel>>() {

            @Override
            public void onNext(List<FileTypeModel> fileTypeModels) {
                fileTypeMutableLiveData.setValue(fileTypeModels);
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        }));
    }

    public void uploadImages(File file, String fileTypeId, String fileName) {
        addSubscribe(mRetrofitHelper.uploadImage2(file).subscribeWith(new ResourceSubscriber<StatusModel>() {

            @Override
            public void onNext(StatusModel statusModel) {
                if (statusModel.getStatus() == 200) {
                    AttachmentModel attachmentModel = new AttachmentModel(statusModel.getId(), fileName, fileTypeId);
                    attachmentModelMutableLiveData.setValue(attachmentModel);
                }
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        }));
    }

    /**
     * 添加跟进
     */
    public void addOrderFollow(OrderAddFollowParamsModel paramsModel) {
        addSubscribe(mRetrofitHelper.addOrderFollow(new Gson().toJson(paramsModel)).subscribeWith(new ResourceSubscriber<StatusModel>() {

            @Override
            public void onNext(StatusModel statusModel) {
                if (statusModel != null && statusModel.getStatus() == 200) {
                    ToastHelper.show("订单跟进成功");
                    mAddSuccessData.setValue("success");
                } else {
                    ToastHelper.show("订单跟进失败");
                }
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        }));
    }

    public void getTreeList(String keyword) {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("keyword", keyword);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addSubscribe(mRetrofitHelper.getTreeData(requestData).subscribeWith(new ResourceSubscriber<SetShareBean>() {

            @Override
            public void onNext(SetShareBean setShareBean) {
                if (Constant.SUCCESS == setShareBean.getResult()) {
                    treeData.setValue(setShareBean);
                }
            }

            @Override
            public void onError(Throwable t) {
                mErrorData.setValue(t.getMessage());
            }

            @Override
            public void onComplete() {

            }
        }));
    }
}
